/****************************************************
	文件：ReactiveProperty.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/15 21:25:10
	功能：响应式属性
*****************************************************/

using System;

namespace HTUtility
{
    public class ReactiveProperty<T>
    {
        private T mValue;
        private Action<T> mOnValueChanged;
        public T Value
        {
            get => mValue;
            set
            {
                if (mValue.Equals(value) == false)
                {
                    mValue = value;
                    mOnValueChanged?.Invoke(value);
                }
            }
        }
        public void Subscribe(Action<T> cb)
        {
            mOnValueChanged += cb;
        }
        public void Unsubscribe(Action<T> cb)
        {
            if (mOnValueChanged == null) return;
            mOnValueChanged -= cb;
        }
        public ReactiveProperty(T init = default)
        {
            mValue = init;
        }

        public void Reset(T init = default)
        {
            mValue = init;
            mOnValueChanged = null;
        }
    }
}
